const blessed = require('blessed')
const contrib = require('blessed-contrib')

class DashBoard{
  constructor() {
    this.donutData = {}
    this.screen = blessed.screen()
    this.grid = new contrib.grid({rows: 12, cols: 21, screen: this.screen})
    this.donut = this.grid.set(0, 0, 4, 21, contrib.donut,
      {
        label: 'Server list',
        radius: 10,
        arcWidth: 4,
        yPadding: 2,
      }
    )

    this.log = this.grid.set(4, 0, 8, 21, contrib.log, 
      {
        fg: "green",
        selectedFg: "green",
        label: 'Server Log'
      }
    )
    
    this.screen.on('resize', () => {
      this.donut.emit('attach')
      this.screen.render()
    })
  
    this.screen.render()
  }

  updateDonut(data) {
    this.donutData = {
      ...this.donutData,
      [data.label]:data
    }
    const renderList = []
    for (let i in this.donutData) {
      renderList.push(this.donutData[i])
    }
    this.donut.setData(renderList)
    this.screen.render()
  }

  updateLog(data) {
    this.log.log(data)
    this.screen.render()
  }
  
}

module.exports = DashBoard
